<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生" prop="studentId">
        <el-select v-model="queryParams.studentId" placeholder="请选择" :popper-append-to-body="false">
          <el-option
            v-for="item in studentList"
            :key="item.id"
            :label="item.studentName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程" prop="courseIds">
        <el-select v-model="queryParams.courseIdList"  multiple placeholder="请选择" :popper-append-to-body="false">
          <el-option
            v-for="item in courseList"
            :key="item.id"
            :label="item.courseName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上课时间" prop="attendClassTime">
        <el-input
          v-model="queryParams.attendClassTime"
          placeholder="请输入上课时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签约开始时间" prop="signStartDate">
        <el-input
          v-model="queryParams.signStartDate"
          placeholder="请输入签约开始时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签约结束时间" prop="signEndDate">
        <el-input
          v-model="queryParams.signEndDate"
          placeholder="请输入签约结束时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="群名" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入群名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同附件名称" prop="contractFileName">
        <el-input
          v-model="queryParams.contractFileName"
          placeholder="请输入合同附件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:sign:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:sign:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:sign:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:sign:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="signList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="学生ID" align="center" prop="studentId" v-if="false"/>
      <el-table-column label="学生名称" align="center" prop="studentName"/>
      <el-table-column label="课程ID" align="center" prop="courseIds" v-if="false" />
      <el-table-column label="课程名称" align="center" prop="courseNames"/>
      <el-table-column label="上课时间" align="center" prop="attendClassTime" />
      <el-table-column label="签约开始时间" align="center" prop="signStartDate" />
      <el-table-column label="签约结束时间" align="center" prop="signEndDate" />
      <el-table-column label="服务类型" align="center" prop="serviceType" />
      <el-table-column label="群名" align="center" prop="groupName" />
      <el-table-column label="合同附件名称" align="center" prop="contractFileName" />
      <el-table-column label="合同附件" align="center" prop="contractFile" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:sign:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:sign:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改签约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生" prop="studentId">
          <el-select v-model="form.studentId" placeholder="请选择学生信息" :popper-append-to-body="false" style="width: 330px">
            <el-option
              v-for="item in studentList"
              :key="item.id"
              :label="item.studentName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程" prop="courseIdList">
          <el-select v-model="form.courseIdList"  multiple placeholder="请选择课程信息" :popper-append-to-body="false" style="width: 330px">
            <el-option
              v-for="item in courseList"
              :key="item.id"
              :label="item.courseName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceType">
          <el-input v-model="form.serviceType" placeholder="请输入服务类型" style="width: 330px"/>
        </el-form-item>
        <el-form-item label="上课时间" prop="attendClassTime">
          <el-date-picker clearable
                          v-model="form.attendClassTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择上课时间" style="width: 330px">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开始时间" prop="signStartDate" >
          <el-date-picker clearable
                          v-model="form.signStartDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择签约时间"
                          :picker-options="startDatePicker" style="width: 330px">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="signEndDate">
          <el-date-picker clearable
                          v-model="form.signEndDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择签约时间"
                          :picker-options="endDatePicker" style="width: 330px">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="群名" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入群名" style="width: 330px" />
        </el-form-item>
        <el-form-item label="合同附件" prop="contractFile">
          <file-upload v-model="form.contractFile"  :fileType="['doc', 'xls', 'ppt', 'pdf','png', 'jpg', 'jpeg']"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSign, getSign, delSign, addSign, updateSign,courseList,studentList } from "@/api/system/sign";

export default {
  name: "Sign",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 签约表格数据
      signList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentId: null,
        courseIds: null,
        attendClassTime: null,
        signStartDate: null,
        signEndDate: null,
        serviceType: null,
        groupName: null,
        contractFileName: null,
        contractFile: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        studentId  : [
          { required: true, message: '请选择学生信息', trigger: 'blur' }
        ],
        courseIdList : [
          { required: true, message: '请选择课程信息', trigger: 'blur' }
        ],
        serviceType : [
          { required: true, message: '请输入服务类型', trigger: 'blur' },
          { min: 1, max: 30, message: '服务类型长度必须介于 1 和 30 之间', trigger: 'blur' }
        ],
        attendClassTime: [
          { required: true, message: '请选择上课时间', trigger: 'blur' }
        ],
        signStartDate : [
          { required: true, message: '请选择签约开始时间', trigger: 'blur' }
        ],
        signEndDate: [
          { required: true, message: '请选择签约结束时间', trigger: 'blur' }
        ],
        groupName : [
          { required: true, message: '请输入微信群名', trigger: 'blur' },
          { min: 1, max: 30, message: '微信群名长度必须介于 1 和 30 之间', trigger: 'blur' }
        ],

      },
      //课程名称
      courseList:[],
      //课程名称
      studentList:[],
      startDatePicker: this.beginDate(),
      endDatePicker: this.processDate()
    };
  },
  created() {
    this.getList();
    this.getCourseList();
    this.getStudentList();
  },
  methods: {
    /** 查询签约列表 */
    getList() {
      this.loading = true;
      listSign(this.queryParams).then(response => {
        this.signList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getCourseList(){
      courseList().then(res => {
        console.log(res)
        if(res.code == 200){
          this.courseList = res.data
        }else{
          console.log("查询不到数据")
        }
      }).catch(function (error) {
        console.log(error)
      })
    },
    getStudentList(){
      studentList().then(res => {
        console.log(res)
        if(res.code == 200){
          this.studentList = res.data
        }else{
          console.log("查询不到数据")
        }
      }).catch(function (error) {
        console.log(error)
      })
    },
    beginDate(){
      const self = this
      return {
        disabledDate(time){
          if (self.form.signEndDate) {  //如果结束时间不为空，则小于结束时间
            return new Date(self.form.signEndDate).getTime() < time.getTime()
          } else {
            // return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
          }
        }
      }
    },
    processDate() {
      const self = this
      return {
        disabledDate(time) {
          if (self.form.signStartDate) {  //如果开始时间不为空，则结束时间大于开始时间
            return new Date(self.form.signStartDate).getTime() > time.getTime()
          } else {
            // return time.getTime() > Date.now()//开始时间不选时，结束时间最大值小于等于当天
          }
        }
      }
    },
      // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentId: null,
        courseIds: null,
        attendClassTime: null,
        signStartDate: null,
        signEndDate: null,
        serviceType: null,
        groupName: null,
        contractFileName: null,
        contractFile: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        courseIdList:[]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加签约";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSign(id).then(response => {
        this.form = response.data;
        this.$set(this.form, "courseIdList", response.courseIdList);
        this.open = true;
        this.title = "修改签约";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSign(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            console.log(this.form);

            addSign(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除签约编号为"' + ids + '"的数据项？').then(function() {
        return delSign(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sign/export', {
        ...this.queryParams
      }, `sign_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
